package com.lovo.sheyingback.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.sheyingback.bean.Function;
import com.lovo.sheyingback.bean.Role;
import com.lovo.sheyingback.bean.User;
import com.lovo.sheyingback.dao.FunctionMapper;
import com.lovo.sheyingback.dao.RoleMapper;
import com.lovo.sheyingback.service.UserService;
import com.lovo.sheyingback.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author BDK-THINKPAD
* @description 针对表【t_user(用户表)】的数据库操作Service实现
* @createDate 2023-07-24 21:31:45
*/
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private FunctionMapper functionMapper;

    @Override
    public PageInfo<User> listUser(Integer current, Integer pageSize) {
        PageHelper.startPage(current,pageSize);
        List<User> users = userMapper.listUser();
        return new PageInfo<>(users);
    }

    @Override
    public User getByUsername(String username) {
        return userMapper.getByUsername(username);
    }

    @Override
    public Map listUserRole(Integer userId) {
        List<Role> userRoles = roleMapper.listRoleByUserId(userId);
        List<Role> allRoles = roleMapper.listRole();
//        List<Role> noAssignRoles = allRoles.stream()
//                .filter(role -> Collections.binarySearch(userRoles,role,(a,b) -> a.getId() - b.getId()) < 0)
//                .collect(Collectors.toList());
        return Map.of("all",allRoles,"assign",userRoles);
    }

    @Override
    public Map listUserFunction(Integer userId) {
        List<Function> userFunctions = functionMapper.listFunctionByUserId(userId);
        List<Function> allFunctions = functionMapper.listFunction();
//        List<Function> noAssignFunctions = allFunctions.stream()
//                .filter(function -> Collections.binarySearch(userFunctions,function,(a,b) -> a.getId() - b.getId()) < 0)
//                .collect(Collectors.toList());
        return Map.of("all",allFunctions,"assign",userFunctions);
    }

    @Override
    public User login(User user) {
        return userMapper.getByUsernameAndPwd(user);
    }

    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }

    @Override
    @Transactional
    public int assignUserRole(Integer userId, Integer[] roleIds) {
        userMapper.deleteUserRole(userId);
        for(Integer roleId : roleIds){
            userMapper.insertUserAndRole(userId,roleId);
        }
        return 1;
    }

    @Override
    @Transactional
    public int assignUserFunction(Integer userId, Integer[] functionIds) {
        userMapper.deleteUserFunction(userId);
        for(Integer functionId : functionIds){
            userMapper.insertUserAndFunction(userId,functionId);
        }
        return 1;
    }

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int update(User user) {
        return userMapper.update(user);
    }

    @Override
    @Transactional
    public int delete(Integer id) {
        userMapper.deleteUserRole(id);
        userMapper.deleteUserFunction(id);
        return userMapper.delete(id);
    }
}




